package com.maiji.cloud.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.entities.login.*;
import com.maiji.cloud.entities.shopingmall.DiscountCouponLog;
import com.maiji.cloud.entities.shopingmall.TaskLog;
import com.maiji.cloud.mapper.SignInDeployMapper;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.response.*;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SignInDeployServiceImpl extends ServiceImpl<SignInDeployMapper, SignInDeployEntity> implements SignInDeployService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public BaseDataResDto<List<SignInDeployEntity>> findAllSignInDeploys () {
        EntityWrapper<SignInDeployEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.orderBy("day", true);
        List<SignInDeployEntity> signInDeploys = selectList(entityWrapper);
        return new BaseDataResDto<List<SignInDeployEntity>>(Status.SUCCESS).setData(signInDeploys);
    }

    @Override
    public BaseResDto editSignInDeploy (SignInDeployEntity signInDeploy) {
        if (StringUtil.isBlank(signInDeploy.getUuId()) && ! insert(signInDeploy.setUuId(UUID_MD5.getUUID())))
            return new BaseResDto(Status.ERROR);
        else if (! updateById(signInDeploy)) return new BaseResDto(Status.ERROR);
        mongoTemplate.save(signInDeploy, SignInDeployEntity.TABLE_NAME);
        return new BaseResDto(Status.SUCCESS);
    }

    @Override
    public BaseDataResDto<List<SignInDeployEntity>> findAllSignInDeployLogs(List<Date> updateDates) {
        Query query = new Query().with(new Sort(new Sort.Order(Sort.Direction.DESC,"editDate")));
        if (updateDates != null) {
            Date startDate = updateDates.size() > 0 ? updateDates.get(0): null;
            Criteria updateDateCriteria = Criteria.where("editDate");
            Boolean updFlag = false;
            if (startDate != null) {
                updateDateCriteria = updateDateCriteria.gte(startDate);
                updFlag = true;
            }
            Date endDate = updateDates.size() > 1 ? updateDates.get(1): null;
            if (endDate != null) {
                updateDateCriteria = updateDateCriteria.lte(endDate);
                updFlag = true;
            }
            if (updFlag) query.addCriteria(updateDateCriteria);
        }
        List<SignInDeployEntity> taskLogs = mongoTemplate.find(query, SignInDeployEntity.class, SignInDeployEntity.TABLE_NAME);
        return new BaseDataResDto<List<SignInDeployEntity>>(Status.SUCCESS).setData(taskLogs);
    }

}
